package com.cgbl.cg.view

import android.content.Context
import android.text.TextUtils
import androidx.appcompat.widget.AppCompatEditText
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.widget.doOnTextChanged
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.TimeUtils
import com.cgbl.cg.R
import com.cgbl.cg.adapter.WorkCommentChildAdapter
import com.lxj.xpopup.core.BottomPopupView
import com.robot.base.model.CommentBean
import com.robot.base.utils.pic.ImageLoaderUtils
import com.robot.base.view.CgTextView

class WorkCommentChildPop(context: Context, var bean: CommentBean, var list: List<CommentBean>) :
    BottomPopupView(context) {

    private var confirmClickListener: (id: String, content: String, accountId: String) -> Unit =
        { id, content, accountId -> }
    private var detailClickListener: (bean: CommentBean) -> Unit = {}

    private lateinit var editContent: AppCompatEditText
    private lateinit var tvFromName: CgTextView
    private lateinit var tvConfirm: CgTextView
    private lateinit var mRvCollect: RecyclerView
    private lateinit var workCommentAdapter: WorkCommentChildAdapter
    private lateinit var ivBack: AppCompatImageView
    private lateinit var itemIvAvatar: AppCompatImageView
    private lateinit var itemTvName: CgTextView
    private lateinit var itemTvTime: CgTextView
    private lateinit var itemTvContent: CgTextView

    private var id = ""
    private var name = ""
    private var accountId = ""

    override fun getImplLayoutId(): Int {
        return R.layout.app_pop_work_comment_child
    }

    override fun initPopupContent() {
        super.initPopupContent()
        ivBack = findViewById(R.id.iv_back)
        tvConfirm = findViewById(R.id.tv_confirm)
        mRvCollect = findViewById(R.id.mRv)
        tvFromName = findViewById(R.id.tv_from_name)
        itemIvAvatar = findViewById(R.id.item_iv_avatar)
        itemTvName = findViewById(R.id.item_tv_name)
        itemTvTime = findViewById(R.id.item_tv_time)
        itemTvContent = findViewById(R.id.item_tv_content)
        ImageLoaderUtils.displayCirclePortrait(context, itemIvAvatar, bean.fromAvatar)
        itemTvName.text = bean.fromNickName ?: ""
        itemTvTime.text = TimeUtils.millis2String((bean.createTime ?: 0L))
        itemTvContent.text = bean.content ?: ""
        id = bean.id ?: ""
        accountId = bean.fromAccountId ?: ""
        name = bean.fromNickName ?: ""
        tvFromName.text = "回复${name}:"
        editContent = findViewById(R.id.edit_content)
        workCommentAdapter = WorkCommentChildAdapter()
        mRvCollect.adapter = workCommentAdapter
        workCommentAdapter.setNewInstance(list as MutableList<CommentBean>?)

        workCommentAdapter.setOnItemClickListener { adapter, view, position ->
            tvFromName.text = "回复${workCommentAdapter.data[position].fromNickName}:"
            accountId = workCommentAdapter.data[position].fromAccountId ?: ""
        }

        editContent.doOnTextChanged { text, start, before, count ->
            tvConfirm.isEnabled = !TextUtils.isEmpty(text)
        }

        ivBack.setOnClickListener {
            dismiss()
        }
        tvConfirm.setOnClickListener {
            confirmClickListener.invoke(
                id, editContent.text.toString(), accountId
            )
        }
    }

    fun setConfirmClickListener(click: (id: String, content: String, accountId: String) -> Unit): WorkCommentChildPop {
        confirmClickListener = click
        return this
    }

    fun setDetailClickListener(click: (bean: CommentBean) -> Unit): WorkCommentChildPop {
        detailClickListener = click
        return this
    }
}